import 'dart:async';

import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/api.dart';
import 'package:qgchat/http/models/callRecordModel.dart';

import '../blocProvider.dart';

/// 通话记录bloc
class CallRecordsBlog implements BlocBase {

  bool isFirstLoading = true; // 是否第一次加载
  int page = 0;
  final int limit = 10;
  List<CallRecordModel> lists = [];

  final List<CallRecordModel> defaultData = [];
  final String token;

  CallRecordsBlog(this.token) {
    getCallRecordLists();
  }

  @override
  void dispose() {
    _callRecords.close();
  }

// Stream ----------------------------------------------------------------------
  /// 通话记录列表
  StreamController<List<CallRecordModel>> _callRecords = StreamController.broadcast();
  Sink<List<CallRecordModel>> get callRecordsSink => _callRecords.sink;
  Stream<List<CallRecordModel>> get callRecordsStream => _callRecords.stream;

// method ----------------------------------------------------------------------
  /// 获取通话记录列表
  void getCallRecordLists() async {
    page += 1;
    List<CallRecordModel> result = await UserApis.getCallRecordLists(token, page, limit);
    if (result.isNotEmpty) {
      isFirstLoading = false;
      lists.addAll(result);
      callRecordsSink.add(lists);
    } else {
      if (!isFirstLoading) showToast("已经没有了喔~ww");
      isFirstLoading = false;
    }
  }

  /// 刷新
  void onRefresh() {
    page = 0;
    lists = [];
    getCallRecordLists();
  }

}